#!/bin/sh

APP_TARGET_DIR_NAME="LatentSync"

CONDA_PREFIX="./venv"
echo "lmdprogress:10"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/alias.sh"
echo "lmdprogress:20"

export CURRENT_APP_PATH="${LMD_APPS_DIR}/${APP_TARGET_DIR_NAME}"
echo $CURRENT_APP_PATH


if [ -d "$CURRENT_APP_PATH" ]; then
    echo "App Dir: "$CURRENT_APP_PATH
else
    echo "Error, App Dir dose not exist $CURRENT_APP_PATH"
    echo "lmdprogress:100"
    exit 1
fi

echo "lmdprogress:40"

cd "$CURRENT_APP_PATH"


if [ -d "$CONDA_PREFIX" ]; then
    echo "found: ./venv"
    echo "remove ./venv"
    conda env remove -p "$CONDA_PREFIX" -y
    echo "removed"
else
    echo "./venv dose not exist"
fi
echo "lmdprogress:50"


if [ -d "$CURRENT_APP_PATH" ]; then
    # remove models
    echo 'remove models'

    MODELS_DIR="./checkpoints"
    echo "App Dir: "$CURRENT_APP_PATH
    rm -rf $MODELS_DIR

    echo "lmdprogress:60"

    TEMP_HF_HOME="./tmp_hf_home"
    echo "App Dir: "$TEMP_HF_HOME
    rm -rf $TEMP_HF_HOME

    echo "lmdprogress:70"
    
    TORCH_HOME="./tmp_torch_home"
    echo "App Dir: "$TORCH_HOME
    rm -rf $TORCH_HOME

    echo "lmdprogress:80"

    # remove app files
    cd "$LMD_APPS_DIR"
    echo 'remove app files'
    rm -rf "$CURRENT_APP_PATH"
    echo "lmdprogress:100"
fi

